<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">
<section class="content-header">
    <h1>工艺卡转序-申请质检</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" class="form-horizontal" onkeydown="if(event.keyCode==13)return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group margin-bottom-none"
                                     style="margin-left:0px;margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        用户名
                                    </label>
                                    <label class="control-label" th:text="${user.name}">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        部门
                                    </label>
                                    <label class="control-label" th:text="${user.deptName}==null?'':${user.deptName}">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        车间
                                    </label>
                                    <label class="control-label"
                                           th:text="${user.branchName}==null?'':${user.branchName}">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="row" shiro:hasPermission="tran:receive:edit">
                            <div class="form-inline">
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        生产令编号
                                    </label>
                                    <label class="control-label">
                                        <select id="select-prodNo" class="form-control" style="width: 140px;text-align: center">

                                        </select>
                                    </label>
                                    <label class="control-label">
                                        类型
                                    </label>
                                    <label class="control-label">
                                        <select id="select-special" class="form-control select2" style="width: 140px;text-align: center">

                                        </select>
                                    </label>
                                    <label class="control-label">
                                        部分
                                    </label>
                                    <label class="control-label">
                                        <select id="select-part" class="form-control select2" style="width: 200px;text-align: center">

                                        </select>
                                    </label>
                                    <label class="control-label">
                                        图号
                                    </label>
                                    <label class="control-label">
                                        <select id="select-drawNo" class="form-control select2" style="width: 300px;text-align: center">

                                        </select>
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="row" shiro:hasPermission="tran:testings:edit">
                            <div class="form-inline">
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        二维码区域
                                    </label>
                                    <label class="control-label">
                                        <input title="按回车键，即可获取转序内容。" id="QRCode" class="form-control" name="QRCode"
                                               onkeyup="loadData()" onfocus="showTips()" onblur="hideTips()"
                                               style="width: 500px;" value=""/>
                                    </label>
                                    <button type="button" class="btn btn-success" id="btn-search" style="margin-top: 12px;"
                                            onclick="loadData2()">
                                        <span class="Bold">搜索</span>
                                    </button>
                                    <label id="tips" class="control-label" style="color:red;display: none;">
                                        按回车键，即可获取转序内容。
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div id="prodOrdAndProjectInfo" class="row" style="display: none">
                            <div class="form-inline">
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        项目编号
                                    </label>
                                    <label id="proCode" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        项目名称
                                    </label>
                                    <label id="proName" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        项目数量
                                    </label>
                                    <label id="proQty" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none"
                                     style="margin-left:0px;margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        生产令编号
                                    </label>
                                    <label id="prodNo" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        生产令数量
                                    </label>
                                    <label id="prodNum" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        需方
                                    </label>
                                    <label id="customer" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        类型
                                    </label>
                                    <label id="prodTyp" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        是否重点
                                    </label>
                                    <label id="prodIsKey" class="control-label">
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:0px;margin-right: 25px;">
                                    <label class="control-label">
                                        交货日期
                                    </label>
                                    <label id="prodDelDat" class="control-label">
                                    </label>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                    <div id="noData1" class="col-md-12"
                         style="text-align: center;vertical-align: middle;display: none;"><h2>查无记录</h2></div>
                    <div id="noData2" class="col-md-12"
                         style="text-align: center;vertical-align: middle;display: none;"><h2>该零件已全部转序</h2></div>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table;
    //  /** 状态 1=未开始 2=执行中 3=部分转序 4=全部转序 */
    let status = ['', '未开始', '执行中', '部分转序', '全部转序'];
    let statusClass = ['', 'label-default', 'label-warning', 'label-success', 'label-success'];
    $(function () {
        loadTableData();
        /** 生产令号下拉选 */
        js.post({
            url: ctx + "transposition/transpositionRec/getRecProdNoList",
            data: {},
            success: function (result) {
                $("#select-prodNo").html('').select2();
                let select2Html = '<option value=""></option>';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                $("#select-prodNo").html(select2Html).select2();
                // $("#select-prodNo").val(result.data[0].id).select2();
                // $("#select-prodNo").val('');
                // getSpecials();
            }
        });

        /** 项目编号下拉框选中 */
        $("#select-prodNo").on("select2:select", function () {
            getSpecials();
        });

        /** 类型下拉框选中 */
        $("#select-special").on("select2:select", function () {
            getParts();
        });
        /** 部分下拉框选中 */
        $("#select-part").on("select2:select", function () {
            getDrawNos();
        });
        /** 图号下拉框选中 */
        $("#select-drawNo").on("select2:select", function () {
            if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                $("#QRCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|1');
            } else {
                $("#QRCode").val('');
            }
            loadData2();
        });
    });

    /**
     * 显示提示
     */
    function showTips() {
        $("#tips").show();
    }

    /**
     * 隐藏提示
     */
    function hideTips() {
        $("#tips").hide();
    }

    /**
     * 获取类型列表
     */
    function getSpecials() {
        js.post({
            url: ctx + "transposition/transpositionRec/getRecProdNoSpecialList",
            data: {prodNo: $("#select-prodNo").val()},
            success: function (result) {
                $("#select-special").empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-special").html(select2Html).select2();
                    $("#select-special").val(result.data[0].id).select2();
                }
                getParts();
            }
        });
    }

    /**
     * 获取部分列表
     */
    function getParts() {
        js.post({
            url: ctx + "transposition/transpositionRec/getRecProdNoPartList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val()
            },
            success: function (result) {
                $("#select-part").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-part").html(select2Html).select2();
                    $("#select-part").val(result.data[0].id).select2();
                }
                getDrawNos();
            }
        });
    }

    /**
     * 获取图号列表
     */
    function getDrawNos() {
        js.post({
            url: ctx + "transposition/transpositionRec/getRecProdNoDrawNoList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val(),
                partId: $("#select-part").val()
            },
            success: function (result) {
                $("#select-drawNo").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-drawNo").html(select2Html).select2();
                    $("#select-drawNo").val(result.data[0].id).select2();
                }
                if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                    $("#QRCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|1');
                } else {
                    $("#QRCode").val('');
                }
                loadData2();
            }
        });
    }


    /**
     * 检测回车
     */
    function loadData() {
        let ev = window.event || e;
        let keyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
        if (keyCode == 13) {
            let qrCodeContent = $("#QRCode").val();
            if (qrCodeContent === '') {
                js.modal.warning("请扫描二维码！");
            } else {
                js.table.refresh(table);
                $.post(
                    ctx + 'transposition/transpositionRec/getProjectAndProdOrdInfo',
                    {
                        qrCode: $("#QRCode").val()
                    },
                    function (res) {
                        if(res.data!=null && res.data != undefined){
                            $("#proCode").text(res.data.proCode);
                            $("#proName").text(res.data.proName);
                            $("#proQty").text(res.data.proQty);
                            $("#prodNo").text(res.data.prodNo);
                            $("#prodNum").text(res.data.prodNum);
                            $("#customer").text(res.data.customer);
                            $("#prodTyp").text(res.data.prodTyp);
                            $("#prodIsKey").text(res.data.prodIsKey);
                            $("#prodDelDat").text(res.data.prodDelDat);
                            $("#prodOrdAndProjectInfo").show();
                        }else {
                            $("#prodOrdAndProjectInfo").hide();
                        }
                    }
                );
            }
        }
    }

    /**
     * 检测回车
     */
    function loadData2() {
        let qrCodeContent = $("#QRCode").val();
        if (qrCodeContent === '') {
            js.modal.warning("请扫描二维码！");
        } else {
            js.table.refresh(table);
            $.post(
                ctx + 'transposition/transpositionRec/getProjectAndProdOrdInfo',
                {
                    qrCode: $("#QRCode").val()
                },
                function (res) {
                    if(res.data!=null && res.data != undefined){
                        $("#proCode").text(res.data.proCode);
                        $("#proName").text(res.data.proName);
                        $("#proQty").text(res.data.proQty);
                        $("#prodNo").text(res.data.prodNo);
                        $("#prodNum").text(res.data.prodNum);
                        $("#customer").text(res.data.customer);
                        $("#prodTyp").text(res.data.prodTyp);
                        $("#prodIsKey").text(res.data.prodIsKey);
                        $("#prodDelDat").text(res.data.prodDelDat);
                        $("#prodOrdAndProjectInfo").show();
                    }else {
                        $("#prodOrdAndProjectInfo").hide();
                    }
                }
            );
        }
    }

    /**
     * 申请质检
     */
    function operApplyCheck(obj, id, applyNum, index) {
        let row = $("#bootstrap-table").bootstrapTable('getRowByUniqueId', id);
        js.post({
            url: ctx + "transposition/applyCheck/apply",
            data: {
                id: id,
                applyNum: applyNum
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                    $.post(
                        ctx + 'transposition/applyCheck/getNewMayApplyNum',
                        {
                            QRCode: $("#QRCode").val()
                        },
                        function (res) {
                            let mayApplyNum = res.data;
                            $(obj).parent().parent().find("input[name='applyNum']").attr('data-max', mayApplyNum);
                            saveData(index, 'mayApplyNum', mayApplyNum);
                        }
                    );
                }

            }
        });
    }

    function loadTableData() {
        $('#bootstrap-table').bootstrapTable('destroy');//这里必须要添加这个销毁，否则新增、修改、查看的切换可编辑列表中的数据可能加载出现问题。
        /**
         * 加载列表
         */
        table = js.table.init({
            url: ctx + "transposition/applyCheck/list",
            showColumns: false,
            showExport: false,
            editable: true,//开启编辑模式
            uniqueId: 'id',
            onLoadSuccess: function (data) {
                if($("#QRCode").val().trim().length>0 && data.rows.length===0){
                    $.post(
                        ctx + 'transposition/applyCheck/completeCount',
                        {
                            QRCode: $("#QRCode").val()
                        },
                        function (res) {
                            if(res.data===0){
                                $("#noData1").show();
                                $("#noData2").hide();
                                $("#bootstrap-table").hide();
                            }else{
                                $("#noData1").hide();
                                $("#noData2").show();
                                $("#bootstrap-table").hide();
                            }
                        }
                    );
                }else{
                    $("#noData1").hide();
                    $("#noData2").hide();
                    $("#bootstrap-table").show();
                }
            },
            columns: [
                {
                    title: '序号', width: '60',
                    formatter: function (value, row, index, field) {
                        var pageSize = table.bootstrapTable('getOptions').pageSize;
                        var pageNumber = table.bootstrapTable('getOptions').pageNumber;
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    title: '生产令号', field: 'prodNo', width: '110',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '项目名称', field: 'proName',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '类型', field: 'special', width: '90',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '部分', field: 'part', width: '150',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '图号', field: 'drawNo', width: '90',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '零件名称', field: 'drawName', width: '90',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '零件数量',
                    field: 'drawNum',
                    width: '90',
                    edit: {type: 'text'},
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '工艺路线', field: 'route', width: '90',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '申请质检数量', field: 'applyNum', width: '150',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<button onclick="add(this)" class="btn btn-success btn-xs"><i class="fa fa-plus"></i></button><input name="applyNum" oninput="formatNumber(' + index + ',this)" onchange="formatNumber(' + index + ',this)" data-max="' + row.mayApplyNum + '" data-id = "' + row.id + '" style="width: 50px" value="' + row.applyNum + '" /><button onclick="reduce(this)" class="btn btn-danger btn-xs" ><i class="fa fa-minus"></i></button>';
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '路线状态', field: 'status', width: '90',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            //  /** 状态 1=未开始 2=执行中 3=部分转序 4=全部转序 */
                            return '<label class="badge ' + statusClass[value] + '">' + status[value] + '</label>';
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '操作', width: '140',
                    formatter: function (value, row, index) {
                        let elements = new Array();
                        if (hasP('tran:testings:commit')) {
                            if ((row.status === 2 || row.status === 3) && row.applyNum > 0) {
                                elements.push('<button class="btn btn-success btn-xs" onclick="operApplyCheck(this,' + row.id + ',' + row.applyNum + ',' + index + ')">申请质检</button>');
                            }
                        }
                        return elements.join('');
                    }
                }
            ]
        });
    }

    /** 保存数据并更新到表格UI页面 */
    function saveData(index, field, value) {
        table.bootstrapTable('updateCell', {
            index: index,       //行索引
            field: field,       //列名
            value: value        //cell值
        })
    }

    /**
     * 增加
     */
    function add(obj) {
        let next = $(obj).next("input[name='applyNum']");
        next.val(parseInt(next.val()) + 1);
        next.attr('style', 'width: 50px');
        next.change();
    }

    /**
     * 减少
     */
    function reduce(obj) {
        let prev = $(obj).prev("input[name='applyNum']");
        if (parseInt(prev.val()) > 0) {
            prev.val(parseInt(prev.val()) - 1);
            prev.attr('style', 'width: 50px');
            prev.change();
        }
    }

    /**
     * 验证输入有效的整数
     */
    function formatNumber(index, obj) {
        obj.value = obj.value.replace(/[^\d]/g, "");
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\*$/, '$1$2.$3');//只能输入六个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = obj.value;
        }
        //如果上去就写- 自动替换
        if (obj.value.indexOf("-") >= 0) {
            obj.value = obj.value.replace('-', "");
        }
        if (obj.value == '00') {
            obj.value = obj.value = '0';
        }
        //如果输入023直接变成23;自动去除整数部分的0
        if (obj.value.length > 2 && obj.value.indexOf(".") < 0) {
            if (obj.value.indexOf("0") == 0) {
                obj.value = obj.value.substr(1, obj.value.length - 1);
            }
        }
        getNewMayApplyNum(index, $(obj).attr('data-id'), obj);
    }

    /** 获取最新的可申请数量 */
    function getNewMayApplyNum(index, id, obj) {
        $.post(
            ctx + 'transposition/applyCheck/getNewMayApplyNum',
            {
                id: id
            },
            function (res) {
                let mayApplyNum = res.data;
                $(obj).attr('data-max', mayApplyNum);
                saveData(index, 'mayApplyNum', mayApplyNum);
                if (parseInt($(obj).val()) > parseInt($(obj).attr('data-max'))) {
                    $(obj).val($(obj).attr('data-max'));
                }
                saveData(index, 'applyNum', $(obj).val());
            }
        );
    }
</script>
</body>
</html>